package com.example.demo.controller;

import com.example.demo.entity.StudentInfo;
import com.example.demo.service.StudentService;
import com.example.demo.service.impl.PageRequest;
import com.example.demo.service.impl.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/stu")
@CrossOrigin
public class StudentController {

    @Autowired
    private StudentService studentService;

    @GetMapping("/abc")
    public String getStudent() {
        return "student20250517";
    }

    // 修改为分页查询接口
    @GetMapping("/list")
    public ResponseEntity<PageResult<StudentInfo>> getStudentList(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {

        PageRequest pageRequest = new PageRequest();
        pageRequest.setPageNum(page);
        pageRequest.setPageSize(size);

        return ResponseEntity.ok(studentService.getStudentInfoByPage(pageRequest));
    }

    @GetMapping("/find")
    public ResponseEntity<StudentInfo> getStudentById(@RequestParam Integer id) {
        return ResponseEntity.ok(studentService.getStudentInfoById(id));
    }

    @DeleteMapping("/delete")
    public ResponseEntity<Integer> deleteStudentById(@RequestParam Integer id) {
        return ResponseEntity.ok(studentService.deleteStudentInfoById(id));
    }

    @PostMapping("/insert")
    public ResponseEntity<Integer> insertStudentInfo(@RequestBody StudentInfo studentInfo) {
        System.out.println("接收到的数据：" + studentInfo);
        return ResponseEntity.ok(studentService.insertStudentInfo(studentInfo));
    }

    @PutMapping("/update")
    public ResponseEntity<Integer> updateStudentInfo(@RequestBody StudentInfo studentInfo) {
        System.out.println("接收到的数据：" + studentInfo);
        return ResponseEntity.ok(studentService.updateStudentInfo(studentInfo));
    }
}